<template>
	<view class="container">
		<view class="user-section">
			<image class="bg" src="/static/user-bg.jpg"></image>
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="userData.profilePhoto || '/static/missing-face.png'"></image>
				</view>
				<view class="info-box">
					<text class="username">{{userData.nickName || '游客'}}</text>
				</view>
			</view>
			<view class="vip-card-box">
				<image class="card-bg" src="/static/vip-card-bg.png" mode=""></image>
				<view class="b-btn" @click="goVIP">
					会员中心<u-icon name="arrow-right" color="#ffd400"></u-icon>
				</view>
				<view class="tit">
					<img style="width: 40rpx;height: 40rpx;margin-right: 20rpx;" v-if="userData.levelIcon"
						:src="userData.levelIcon" alt="" />
					<text v-else class="yticon icon-iLinkapp-"></text>
					{{userData.name}}
				</view>
				<view class="progressDiv">
					<view class="progressDivLeft">
						{{userData.level}}
					</view>
					<view class="progressItem" :style="{ width:  `${wigthData}%` }">
						<span>{{wigthData}}%</span>
					</view>
					<view class="progressDivRight">
						{{userData.level - 0 + 1}}
					</view>
				</view>
			</view>
		</view>

		<view class="cover-container" :style="[{
				transform: coverTransform,
				transition: coverTransition
			}]" @touchstart="coverTouchstart" @touchmove="coverTouchmove" @touchend="coverTouchend">
			<image class="arc" src="/static/arc.png"></image>

			<view class="tj-sction">
				<view class="tj-item" @click="goCollect">
					<text class="num">{{userData.personalLike}}</text>
					<text>收藏</text>
				</view>
				<view class="tj-item" @click="goHistory">
					<text class="num">{{userData.browsingHistory}}</text>
					<text>浏览历史</text>
				</view>
				<view class="tj-item">
					<text class="num">{{userData.integral}}</text>
					<text>积分</text>
				</view>
				<view class="tj-item" @click="goCoupon">
					<text class="num">{{userData.discountVoucher}}</text>
					<text>优惠卷</text>
				</view>
			</view>

			<!-- 订单 -->
			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-shouye"></text>
					<text>全部订单</text>
					<text>{{userData.orderAllCount}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-daifukuan"></text>
					<text>待付款</text>
					<text>{{userData.arrearageCount}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-yishouhuo"></text>
					<text>待收货</text>
					<text>{{userData.waitReceiving}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>退款/售后</text>
					<text>{{userData.arrearageCount}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>已完成</text>
					<text>{{userData.completed}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>待出行</text>
					<text>{{userData.waitSetOut}}</text>
				</view>
			</view>

			<!-- 便民服务 -->
			<view class="order-section order-section1">
				<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/wc.png" alt="" />
					</view>
					<text>找卫生间</text>
					<text>{{userData.orderAllCount}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/tc.png" alt="" />
					</view>
					<text>找停车场</text>
					<text>{{userData.arrearageCount}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/yy.png" alt="" />
					</view>
					<text>找医院</text>
					<text>{{userData.waitReceiving}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/jy.png" alt="" />
					</view>
					<text>找加油站</text>
					<text>{{userData.arrearageCount}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/dy.png" alt="" />
					</view>
					<text>查导游</text>
					<text>{{userData.completed}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/lxs.png" alt="" />
					</view>
					<text>查旅行社</text>
					<text>{{userData.waitSetOut}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/gw.png" alt="" />
					</view>
					<text>查购物店</text>
					<text>{{userData.waitSetOut}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="50">
					<view class="">
						<img src="/static/my/jg.png" alt="" />
					</view>
					<text>市场监管</text>
					<text>{{userData.waitSetOut}}</text>
				</view>
			</view>


			<!-- 浏览历史 -->
			<view class="history-section icon">
				<list-cell icon="chat-fill" iconColor="#5fcda2" title="消息中心"
					@eventClick="navTo('/pages/notice/notice')"></list-cell>

				<list-cell icon="map-fill" iconColor="#5fcda2" title="地址管理"
					@eventClick="navTo('/pages/address/address')"></list-cell>

				<list-cell icon="plus-people-fill" iconColor="#5fcda2" title="加入我们"
					@eventClick="navToJoinUs()"></list-cell>

				<list-cell icon="phone-fill" iconColor="#5fcda2" title="联系我们"
					@eventClick="navTo('/pages/contactUs/contactUs')"></list-cell>

				<!-- <list-cell icon="info" iconColor="#5fcda2" title="关于云旅迹"
					@eventClick="navTo('/pages/address/address')"></list-cell> -->

				<list-cell icon="setting-fill" iconColor="#e07472" title="设置" border=""
					@eventClick="navTo('/pages/set/set')"></list-cell>
			</view>
		</view>

		<customTabBar></customTabBar>
	</view>
</template>
<script>
	import {
		personalInformation
	} from '@/api/userCenter.js'
	import listCell from '@/components/mix-list-cell';
	import {
		mapState
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			listCell
		},
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				userData: {},
				wigthData: 0,
				isRoute: ''
			}
		},
		// onLoad() {
		// 	this.getUserData()
		// },
		onShow() {
			if (this.$mp.page.route == 'pages/user/user') {
				this.isRoute = '我的'
			}
			this.getUserData()
		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		methods: {
			// 跳转到优惠卷页面
			goCoupon() {
				uni.navigateTo({
					url: '/pages/coupon/coupon'
				})
			},
			// 跳转到会员页面
			goVIP() {
				uni.navigateTo({
					url: '/pages/VIP/VIP'
				})
			},
			// 获取信息
			async getUserData() {
				let res = await personalInformation()
				if (res.code == 200) {
					this.userData = res.data
					this.wigthData = (res.data.nowGrowth / (res.data.nextGrowthNeeded - 0 + res.data.nowGrowth - 0)) *
						100
				}
			},
			// 跳转到浏览历史页面
			goHistory() {
				uni.navigateTo({
					url: '/pages/history/history'
				})
			},
			// 跳转到收藏页面
			goCollect() {
				uni.navigateTo({
					url: '/pages/collect/collect'
				})
			},

			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navToJoinUs() {
				if (this.userData.storeExit == 2) {
					uni.navigateTo({
						url: `/pages/joinUs/joinUs?phone=${this.userData.phone}`
					})
				} else if (this.userData.storeExit == 1) {
					wx.showToast({
						title: '已成为商家',
						icon: 'success',
						duration: 1000
					})
				} else if (this.userData.storeExit == 0) {
					wx.showToast({
						title: '成为商家申请中',
						icon: 'success',
						duration: 1000
					})
				}
			},
			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				if (url == '/pages/set/set') {
					url = `${url}?passwordStatus=${this.userData.passwordStatus}&phone=${this.userData.phone}`
				}
				uni.navigateTo({
					url
				})
			},

			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}

				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
		}
	}
</script>
<style lang='scss' scoped>
	.container {
		padding-bottom: 150rpx;
		background-color: #f5f5f5;
	}

	.progressDiv {
		width: 100%;
		height: 20rpx;
		border-radius: 10rpx;
		background-color: #ececec;
		padding: 5rpx;
		display: flex;
		align-items: center;
		position: relative;

		.progressDivLeft {
			width: 30rpx;
			height: 30rpx;
			border-radius: 100%;
			background-color: #f7d680;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #f5f5f5;
			font-size: 24rpx;
			position: absolute;
			left: 0;
		}

		.progressDivRight {
			width: 30rpx;
			height: 30rpx;
			border-radius: 100%;
			background-color: #f7d680;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #f5f5f5;
			font-size: 24rpx;
			position: absolute;
			right: 0;
		}

		.progressItem {
			width: 100rpx;
			height: 12rpx;
			background-color: rgb(25, 190, 107);
			border-radius: 6rpx;
			color: red;
			display: flex;
			align-items: center;

			span {
				margin-left: 80%;
				font-size: 24rpx;
				margin-bottom: -50rpx;
			}
		}
	}

	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	.user-section {
		height: 520upx;
		padding: 100upx 30upx 0;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			font-size: $font-lg + 6upx;
			color: $font-color-dark;
			margin-left: 20upx;
		}
	}

	.vip-card-box {
		display: flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0, 0, 0, .7), rgba(0, 0, 0, .8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;
		width: 100%;

		.card-bg {
			position: absolute;
			top: 20upx;
			right: 0;
			width: 380upx;
			height: 260upx;
			z-index: -1;
		}

		.b-btn {
			position: absolute;
			right: 20upx;
			top: 16upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 24upx;
			color: #ffd400;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
			display: flex;
			align-items: center;
		}

		.tit {
			font-size: $font-base+2upx;
			color: #e80000;
			margin-bottom: 28upx;
			display: flex;
			align-items: center;

			.yticon {
				color: #ff0000;
				margin-right: 16upx;
			}
		}

		.e-b {
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}

	.cover-container {
		background: $page-color-base;
		margin-top: -150upx;
		padding: 0 30upx;
		position: relative;
		background: #f5f5f5;
		padding-bottom: 20upx;

		.arc {
			position: absolute;
			left: 0;
			top: -34upx;
			width: 100%;
			height: 36upx;
		}
	}

	.tj-sction {
		@extend %section;

		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #75787d;
		}

		.num {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}

	.order-section1 {
		.order-item {
			width: 20% !important;
		}
	}

	.order-section {
		@extend %section;
		padding: 28upx 0;
		margin-top: 20upx;
		flex-wrap: wrap;
		gap: 20rpx;

		.order-item {
			@extend %flex-center;
			width: 30%;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;

			img {
				width: 40rpx;
				height: 40rpx;
			}
		}

		.yticon {
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #fa436a;
		}

		.icon-shouhoutuikuan {
			font-size: 44upx;
		}
	}

	.history-section {
		padding: 30upx 0 0;
		margin-top: 20upx;
		background: #fff;
		border-radius: 10upx;

		.sec-header {
			display: flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;

			.yticon {
				font-size: 44upx;
				color: #5eba8f;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}

		.h-list {
			white-space: nowrap;
			padding: 30upx 30upx 0;

			image {
				display: inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}
	}
</style>